package cn.ito.zjgsu.domain.wrapper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import cn.ito.zjgsu.domain.UserLogin;
import cn.ito.zjgsu.service.PersonService;
import cn.ito.zjgsu.service.RolePermissionService;
import cn.ito.zjgsu.service.UserService;

@Service
public class UserWrapper {

	private UserLogin userLogin;

	private static PersonService personService;

	public PersonService getPersonService() {
		return personService;
	}

	@Resource
	public void setPersonService(PersonService personService) {
		UserWrapper.personService = personService;
	}

	private static RolePermissionService rolePermissionService;

	public RolePermissionService getRolePermissionService() {
		return rolePermissionService;
	}

	@Resource
	public void setRolePermissionService(RolePermissionService rolePermissionService) {
		UserWrapper.rolePermissionService = rolePermissionService;
	}

	private Map<String, List<String>> rolePermission = null; // key -role value
																// -permissions
	private RoleWrapper roleWrapper;

	private PersonWrapper personWrapper;

	public UserWrapper() {
	};

	public UserWrapper(UserLogin userLogin) {
		this.userLogin = userLogin;
	}

	public RoleWrapper getRoleWrapper() {
		return roleWrapper;
	}

	public void setRoleWrapper(RoleWrapper roleWrapper) {
		this.roleWrapper = roleWrapper;
	}

	public PersonWrapper getPersonWrapper() {
		return personWrapper;
	}

	public void setPersonWrapper(PersonWrapper personWrapper) {
		this.personWrapper = personWrapper;
	}

	public Map<String, List<String>> getRolePermission() {
		return rolePermission;
	}

	public void setRolePermission(Map<String, List<String>> rolePermission) {
		this.rolePermission = rolePermission;
	}

	public UserLogin getUserLogin() {
		return userLogin;
	}

	public void setUserLogin(UserLogin userLogin) {
		this.userLogin = userLogin;
	}

	public String toJsonObjectString() {
		return toJsonObject().toJSONString();
	}

	public JSONObject toJsonObject() {
		JSONObject json = new JSONObject();
		initUserField(json);
		return json;
	}

	private void initUserField(JSONObject json) {
		json.put("JobId", userLogin.getJobId());
		json.put("LoginCreateTime", userLogin.getLoginCreateTime());
		json.put("LoginId", userLogin.getLoginId());
		json.put("Nickname", userLogin.getNickname());
		json.put("Password", userLogin.getPassword());
		json.put("PersonId", userLogin.getPersonId());
		json.put("Username", userLogin.getUsername());
		if(null != personWrapper){
			json.put("person", personWrapper.toJsonObject());
		}
		if(null != roleWrapper){
			json.put("role", roleWrapper.toJsonObjectString());
		}
		if(null != rolePermission){
			json.put("rp", rp2JsonObject(rolePermission));
		}
	}

	private JSONObject rp2JsonObject(Map<String, List<String>> _rolePermission) {
		JSONObject json = new JSONObject();
		if (_rolePermission != null) {
			for (String key : _rolePermission.keySet()) {
				json.put(key, _rolePermission.get(key));
			}
			return json;
		}
		return null;

	}

	public static JSONArray toJsonArray(List<UserWrapper> userWrappers) {
		JSONArray array = new JSONArray();
		for (UserWrapper uw : userWrappers) {
			array.add(uw.toJsonObject());
		}
		return array;
	}

	public static String toJsonArrayString(JSONArray array) {
		return array.toJSONString();
	}

	public static String toJsonArrayString(List<UserWrapper> userWrappers) {
		return toJsonArray(userWrappers).toJSONString();
	}

	public static List<UserWrapper> wrapUserLogin(List<UserLogin> users) {
		List<UserWrapper> userWrappers = new ArrayList<UserWrapper>();
		for (UserLogin userLogin : users) {
			UserWrapper userWrapper = new UserWrapper(userLogin);
			userWrapper.setPersonWrapper(personService.getPersonWrapperById(userLogin.getPersonId()));
			Integer rid = userLogin.getRoleId();
			RoleWrapper roleWrapper = rolePermissionService.getRoleWrapper(rid);
			userWrapper.setRoleWrapper(roleWrapper);
			userWrappers.add(userWrapper);
		}
		return userWrappers;
	}

}
